var tableIns;
layui.use(['jquery', 'form', 'table', 'layer', 'upload'], function () {
    var $ = layui.jquery;
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var upload = layui.upload;
    //加载 数据
    tableIns = table.render({
        elem: '#NumberTable'
        , url: '/reward/numberConifg'
        , toolbar: '#NumberToolBar' //开启头部工具栏，并为其绑定左侧模板
        , title: '商品数据表'
        , height: 'full-280'
        , page: true
        , cols: [[
            {field: 'id', sort: true, title: '标识编码', align: 'center'}
            , {field: 'name', title: '名字', align: 'center'}
            , {field: 'development', title: '部门', align: 'center'}
            , {field: 'major', title: '专业', align: 'center'}
            , {field: 'classes', title: '班级', align: 'center'}
            , {fixed: 'right', title: '操作', toolbar: '#NumberRowBar', align: 'center', width: '200'}
        ]]
        , done: function (res, curr, count) { //处理删除某一页最后一条数据的BUG
            if (res.data.length == 0 && curr != 1) {
                tableIns.reload({
                    page: {
                        curr: (curr - 1)
                    }
                });
            }
        }
    });
    upload.render({
        elem: '.thumbBox'
        , url: '/file/uploadFile'
        , acceptMime: 'image/*'
        , field: 'mf'
        , method: "post"  //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
        , done: function (res, index, upload) {
            var path = res.path;
            // alert(path);
            $('.thumbImg').attr('src', '/file/showImageByPath?path=' + path);
            $('#goodsimg').val(path);
            $('.thumbBox').css("background", "#fff");
        }
    });

    //模糊查询
    form.on("submit(doSearch)", function (data) {
        tableIns.reload({
            where: data.field,
            page: {
                curr: 1
            }
        });
        return false;
    });

    //监听工具条的事件
    table.on("toolbar(NumberTable)", function (obj) {
        switch (obj.event) {
            case 'add':
                openAddLayer();
                break;
            case 'batchDelete':
                batchDelete();
                break;
        }
        ;
    });

    //监听行工具条的事件
    table.on("tool(NumberTable)", function (obj) {
        var data = obj.data; //获得当前行数据
        switch (obj.event) {
            case 'update':
                openUpdateGoodsLayer(data);
                break;
            case 'delete':
                deleteGoods(data);
                break;
        }
        ;
    });

    var mainIndex;
    var url;

    //打开添加的弹出层
    function openAddLayer() {
        mainIndex = layer.open({
            type: 1,
            content: $("#addOrUpdateDiv"),
            area: ['800px', '600px'],
            title: '添加商品',
            success: function () {
                $("#dataFrm")[0].reset();
                url = "";

            }
        });
    }

    //打开修改的弹出层
    function openUpdateGoodsLayer(data) {
        mainIndex = layer.open({
            type: 1,
            content: $("#addOrUpdateDiv"),
            area: ['800px', '600px'],
            title: '修改商品',
            success: function () {
                $("#dataFrm")[0].reset();
                //装载新的数据
                form.val("dataFrm", data);
                url = "";
            }
        });
    }

    form.on("submit(doSubmit)", function (data) {
        $.post(url, data.field, function (res) {
            if (res.code == 200) {
                tableIns.reload();
            }
            layer.msg(res.msg);
            layer.close(mainIndex);
        })
        return false;
    });

    //删除
    function deleteGoods(data) {
        layer.confirm('你确定要删除【' + data.goodsname + '】这个商品吗?', {icon: 3, title: '提示'}, function (index) {
            $.post("", {id: data.id, goodsimg: data.goodsimg}, function (res) {
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
            })
            layer.close(index);
        });
    }
});